<?php

namespace app\store\model;

use think\Model;
use think\model\concern\SoftDelete;

class Goods extends Model
{   
    use SoftDelete;


    protected $deleteTime = 'delete_time';

    protected $schema = [
        'id' => 'int',
        'store_id' => 'int',
        'category_id' => 'int',
        'sort' => 'int',
        'goods_name' => 'string',
        'main_image' => 'string',
        'goods_images' => 'json',
        'tags' => 'string',
        'goods_attr' => 'json',
        'spec_type' => 'int',
        'is_limit_member' => 'int',
        'is_subsidy' => 'int',
        'is_give_integral' => 'int',
        'is_free_task' => 'int',
        'invite_friend_num' => 'int',
        'free_expire_time' => 'int',
        'stock' => 'int',
        'total_sale' => 'int',
        'market_price' => 'float',
        'sell_price' => 'float',
        'subsidy_price' => 'float',
        'restricted_members' => 'string',
        'status' => 'int',
        'create_time' => 'timestamp',
        'update_time' => 'timestamp',
        'delete_time' => 'timestamp', 
    ];

    protected $type = [
        'goods_images' => 'json'
    ];

    protected $readonly = ['goods_attr'];

    
    public function setRestrictedMembersAttr($value)
    {
        return empty($value) ? '' : join('-',$value);
    }

    public function getRestrictedMembersAttr($value)
    {   
        return empty($value) ? [] : explode('-',rtrim($value,'-'));
    }

    public function goodsContent()
    {
        return $this->hasOne(GoodsContent::class);
    }

    public function items()
    {
        return $this->hasMany(GoodsItem::class);
    }

    public function category()
    {
        return $this->belongsTo(GoodsCategory::class,'category_id','id');
    }

    public function tags()
    {
        return $this->belongsToMany(GoodsTag::class,GoodsMidTag::class);
    }
}